package com.endurance.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class Test03ReturnController {


    /**
     * 1.返回值类型-ModelView
     */
    @RequestMapping("/index.do")
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        //modelAndView.setViewName("/WEB-INF/jsp/index.jsp");
        modelAndView.setViewName("index");// 最终的路径：视图解析器中配置的前缀+viewname+后缀
        modelAndView.addObject("name", "zhangsan1");
        return modelAndView;
    }

    /**
     * 2. 返回值为 void 类型
     */
    @RequestMapping("list.do")
    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ServletException, IOException {
        request.setAttribute("list", "从数据库中查询集合信息-list.do");
        request.getRequestDispatcher("/WEB-INF/jsp/list.jsp").forward(request, response);
    }

    /**
     * 2. 返回值为 void 类型
     */
    @RequestMapping("update.do")
    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("update", "更新数据库订单信息");
        // 客户端请求再发送一次， 要加上 request.getContextPath()
        System.out.println(request.getContextPath() + "/list.do");
        // 请求服务器的另一个资源
        response.sendRedirect(request.getContextPath() + "/list.do");
    }

    /**
     * 3. 返回值为String类型 - 返回一个试图名称
     */
    @RequestMapping("list02.do")
    public String list02(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // spring配置文件中的 前缀 + order + 后缀
        return "list";
    }

    /**
     * 3. 返回值为String类型 - 返回一个转发字符串类型
     */
    @RequestMapping("list03.do")
    public String list03(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ServletException, IOException {
        request.setAttribute("list", "从数据库中查询集合信息-list03");
        return "forward:list.do";
    }

    /**
     * 3. 返回值为String类型 - 返回一个重定向字符串类型
     */
    @RequestMapping("list04.do")
    public String list04(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ServletException, IOException {
        return "forward:list.do";
    }


}
